
#include "main.h"
#include "utils.h"

#define LED_W_ON PORTA &= ~(1 << 7)	
#define LED_W_OFF PORTA |= (1 << 7)	
#define LED_A_ON PORTC &= ~(1 << 0)
#define LED_A_OFF PORTC |= (1 << 0)


extern u8 lcdout;

void display_name();
void display_alarms();

void intro();
void init_eeprom();
void setup_io();
void setup_int();

void pin_loop();
void serwis_loop();

void check_keyboard();
void off_routine();

void config_menu();
void load_routine();

void awp_check();
void aku_check();

u8 check_input(u8 force);

void kalib_serwis(u8 mode, u16 value);
/////////////////MEDIA//////////

void show_media(u16 val, u8 flags);
void show_med_low(u16 val);
void show_med_high(u16 val);

void show_rh();
void show_tmp();

extern u16 meas;

void work();
void show_param();
///////////////////////// cal
void kalib_wait();
void int_err();
void kalib_routin();
void cal_low();
void cal_high();
void cal_zero();
void cal_out();

extern i8 calib_ptr_deep;
extern i8 calib_ptr;

extern u16 calib_gas;

#ifndef MEDIA_CH4IR
#define CM_STWYS	0x00
#define CM_STNIS	0x01
#define CM_FAB		0x02
#define CM_WYJ		0x03
#else
#define CM_ZERO		0x00
#define CM_STNIS	0x01
#define CM_STWYS	0x02
#define CM_FAB		0x03
#define CM_WYJ		0x04

#endif

extern volatile u16 TimerH;
#define T1_STOP		TCCR1B = 0

void resetStartTimer();
extern u8 cal_wait_flag;

//////////////////SERWIS/////////////////////////

#define SM_NR		0x00
#define SM_AKU		0x01
#define SM_DATA		0x02
#define SM_KALU		0x03
#define SM_HIGH		0x04
#define SM_LOW		0x05
#define SM_CALF		0x06
#define SM_RESET	0x07
#define SM_ALDEBUG	0x08
#define SM_OUT		0x09

void sr_nr();
void sr_aku();
void sr_data();
void sr_kalu();
void sr_low();
void sr_high();
void sr_calf();
void sr_aldebug();
void sr_actref();
/////////////////////////////

extern i16 p_off;		// power offset kalib
extern i16 a_off;		// aku_offset kalib

extern u16 napa;
extern u16 napb;

extern u16 v_aku;
extern u16 v_pow;
extern u16 v_dc;
extern u16 v_pow;
extern u16 v_tmp;

extern u16 Nr;
#define EEP_NR	1
extern u8 Ksp;
#define EEP_KSP 3
extern u8 Awp;
#define EEP_AWP 4
extern u16 AL1;
#define EEP_AL1 5
extern u16 AL2;
#define EEP_AL2 7
extern u8 Kcl;
#define EEP_KCL 9

extern u16 U1_21;
#define EEP_1_21 10
extern u16 U1_20;
#define EEP_1_20 12
extern u16 U1_40;
#define EEP_1_40 14
extern u16 U1_35;
#define EEP_1_35 16

extern u16 U2_21;
#define EEP_2_21 18
extern u16 U2_20;
#define EEP_2_20 20
extern u16 U2_40;
#define EEP_2_40 22
extern u16 U2_35;
#define EEP_2_35 24

extern u8 uout;
#define EEP_UOUT 36
extern u8 lcdmode;
#define EEP_LCDM 50

extern u16 leg_date;
#define EEP_DATE	42

extern u16 stw1;
extern u16 stw2;
extern u16 stn;
#define EEP_STW1 46
#define EEP_STW2 48
#define EEP_STN 32

#define EEP_SH_FLAG	34

#define EEP_SPRN	54
#define EEP_SPRW	56

#define EEP_PR_RANGE_L 60
#define EEP_PR_RANGE_H 62

#define EEP_PARAM	64

#define EEP_TM_CORR	66
#define EEP_RH_CORR	68

#define EEP_AKU_OFF	70
#define EEP_POW_OFF	72

#define EEP_WORK_TIME	500

extern u16 pr_range_l;
extern u16 pr_range_h;

extern i16 corr_t;
extern i16 corr_rh;

extern u8 param_trans;
extern u8 serwis_flag;

extern u8 pin_ok;
extern u16 ref_pin;

extern u8 Pro;
#define PRO_WORK		0
#define PRO_CONFIG		1
#define PRO_CALIB_WAIT	2
#define PRO_CALIB		3
#define PRO_SERWIS		4
#define PRO_PARAM		5
#define PRO_OFF			6
#define PRO_KALPIN		7


#define VER	"ver3.05"




#define LG_ON	//PORTB |= (1 << 7);
#define LG_OFF	//PORTB &= ~(1 << 7);

#define LR_ON	//PORTB |= (1 << 6);
#define LR_OFF	//PORTB &= ~(1 << 6);

#define LY_ON	//PORTB |= (1 << 5);
#define LY_OFF	//PORTB &= ~(1 << 5);

void set_u_out();
void calc_dac_out();
void dac_set();
void put_to_sleep();

void blink_led();

#define DAC_VCC_ON		{ PORTB |= ( 1 << 3); DDRB |= (1 << 3); }
#define DAC_VCC_OFF		{ DDRB &= ~(1 << 3); PORTB &= ~( 1 << 3); }
#define DAC_CS_HI		PORTB |= ( 1 << 0);
#define DAC_CS_LOW		PORTB &= ~( 1 << 0);
#define DAC_CLK_HI		PORTB |= ( 1 << 1);
#define DAC_CLK_LOW		PORTB &= ~( 1 << 1);
#define DAC_DIN_HI		PORTB |= ( 1 << 4);
#define DAC_DIN_LOW		PORTB &= ~( 1 << 4);

/////////AWP

#define AWP_SET_POWERFLAG		awp_flags |= (1 << 1);
#define AWP_RSET_POWERFLAG		awp_flags &= ~(1 << 1);
#define POWERFLAG	0x02

#define AL1_FLAG	0x04
#define AL2_FLAG	0x08



#define ALARM1_ON		PORTD |= (1 << 6);
#define ALARM2_ON		PORTD |= (1 << 7);
#define ALARM1_OFF		PORTD &= ~(1 << 6);
#define ALARM2_OFF		PORTD &= ~(1 << 7);

#define CUR_SW_HIGH		PORTB |= ( 1 << 7);
#define CUR_SW_LOW		PORTB &= ~( 1 << 7);


#define CODE_INIT	0xFFF2
#define CODE_FACT	0xFFF4
#define CODE_ZERO	0xFFF6		// accepted
#define CODE_SENS	0xFFF8		// accepted
#define CODE_ERR	0xFFF0
#define CODE_SERVC	0xFFFA		// refused to calib s
#define CODE_SERVZ	0xFFFB		// refused to calib z


extern u8 awp_flags;

extern u8 cur;
extern char pin[4];

extern u16 debug_actref;

extern u8 sh_flag;

#ifdef MEDIA_PS

void show_pr_spec();


#endif